<template>
  <div class="container">
    <div class="header1" style="padding: 10px 0px">
      <div style="display: flex; justify-content: space-between">
        <div style="display: flex; justify-content: start">
          <a-button class="hte-btn" @click="reSend()">重新发送</a-button>
          <a-button class="hte-btn" @click="edit()">编辑</a-button>
          <!-- <a-button class="hte-btn" plain @click="handleExport">导出</a-button> -->
          <a-button class="hte-btn" plain @click="handleReload">刷新</a-button>
        </div>
        <div style="display: flex; justify-content: end">
          <a-input-search v-model="searchForm.keyword" @search="handleToSearch" placeholder="请按照关键字进行搜索" />
          <a-button class="hte-btn del-btn" @click="showSearchForm = true">高级查询</a-button>
        </div>
      </div>
      <a-modal
        :visible="showSearchForm"
        width="70vw"
        :bodyStyle="{ padding: '10px' }"
        title="高级查询"
        @ok="handleToSearch"
        @cancel="showSearchForm = false"
      >
        <a-form-model
          ref="ruleForm"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          @keyup.enter.native="handleToSearch"
        >
          <a-row :gutter="24">
            <a-col :span="8">
              <a-form-model-item label="发送状态">
                <a-select v-model="searchForm.status" style="width: 100%" allowClear>
                  <a-select-option value=""> 全部 </a-select-option>
                  <a-select-option value="0">正在发送</a-select-option>
                  <a-select-option value="1">发送成功</a-select-option>
                  <a-select-option value="2">发送失败</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="接收邮箱">
                <a-input allow-clear v-model="searchForm.toEmail" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="邮件标题">
                <a-input allow-clear v-model="searchForm.title" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :span="8">
              <a-form-model-item label="邮件内容">
                <a-input allow-clear v-model="searchForm.content" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-modal>
    </div>
    <div class="content-list">
      <a-table
        :columns="columns"
        :data-source="records"
        rowKey="id"
        bordered
        :pagination="false"
        :customRow="customRow"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template slot="statusRender" slot-scope="text">
          <template v-if="text == 0"> 正在发送 </template>
          <template v-if="text == 1"> 发送成功 </template>
          <template v-if="text == 2"> 发送失败 </template>
        </template>
        <template slot="actionRender" slot-scope="text, record">
          <a-button @click="reset(record)">重新执行</a-button>
          <a-button @click="view(record)">查看参数</a-button>
        </template>
      </a-table>
      <a-pagination
        show-size-changer
        :default-current="1"
        :total="searchForm.total"
        @change="pageChange"
        @showSizeChange="onShowSizeChange"
        :show-total="(total) => `共 ${total} 条记录`"
      />
    </div>
  </div>
</template>
<script>
import { postAction } from '@/api/manage'
import { parseTime } from '@/utils/util'
import { Modal } from 'ant-design-vue'
import { UtilsMixin } from '@/mixins/UtilsMixin'
import { TableMixin } from '@/mixins/TableMixin'
export default {
  mixins: [UtilsMixin, TableMixin],
  components: {
    HomeNav: () => import('@/components/layouts/HomeNav'),
  },
  data() {
    return {
      currUserId: this.$store.getters.userInfo.username,
      userinfo: {
        info: this.$store.getters.userInfo,
        module: 'ContractReportInfo',
      },
      urls: {
        list: '/hteEmailLogs/page',
        reSend: '/hteEmailLogs/reSend',
        edit: '/hteEmailLogs/edit',
      },
      labelCol: { span: 8 },
      wrapperCol: { span: 15 },
      keywordType: '1',
      json: {},
      record: {},
      records: [],
      columns: [
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'statusRender' },
          width: '60px',
          align: 'center',
          ellipsis: true,
        },
        {
          title: '收件邮箱',
          dataIndex: 'toEmail',
          width: '100px',
          align: 'left',
          ellipsis: true,
        },
        {
          title: '邮件标题',
          dataIndex: 'title',
          width: '150px',
          align: 'center',
          ellipsis: true,
        },
        {
          title: '邮件内容',
          dataIndex: 'content',
          align: 'center',
          width: '400px',
          ellipsis: true,
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center',
          width: '100px',
          ellipsis: true,
        },
        {
          title: '发送次数',
          dataIndex: 'sendCount',
          align: 'center',
          width: '100px',
          ellipsis: true,
        },
        {
          title: '发送时间',
          dataIndex: 'sendTime',
          align: 'center',
          width: '100px',
          ellipsis: true,
        },
        {
          title: '发送结果',
          dataIndex: 'remark',
          align: 'center',
          width: '100px',
          ellipsis: true,
        },
        // {
        //   title: '操作',
        //   align: 'center',
        //   width: '180px',
        //   scopedSlots: { customRender: 'actionRender' },
        // },
      ],
      showSearchForm: false,
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  async mounted() {
    this.fetchList()
  },
  methods: {
    fetchList() {
      postAction(this.urls.list, this.searchForm).then((res) => {
        if (res.success) {
          this.records = res.result.records
          this.$set(this.searchForm, 'total', res.result.total)
        } else {
          this.$message.error('查询数据错误:' + res.message)
        }
      })
    },

    handleReload() {
      this.fetchList()
    },
    handleToSearch() {
      this.searchForm.pageNo = 1
      this.showSearchForm = false
      this.fetchList()
    },
    handleExport() {
      this.searchForm.selectedIds = this.selectedRowKeys.join(',')
      let url = process.env.VUE_APP_API_BASE_URL + '/infrastructure/export?1=1'
      this.handleExportXls(parseTime(new Date(), '{y}{m}{d}') + '《基建现场管理单据列表》', url, this.searchForm)
    },
    pageChange(pageNo, pageSize) {
      this.$set(this.searchForm, 'pageSize', pageSize)
      this.$set(this.searchForm, 'pageNo', pageNo)
      this.fetchList()
    },
    onShowSizeChange(pageNo, pageSize) {
      this.searchForm.pageNo = 1
      this.searchForm.pageSize = pageSize
      this.fetchList()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    customRow(record) {
      return {
        on: {
          dblclick: (event) => {
            if (!this.selectedRowKeys) {
              this.selectedRowKeys = []
              this.selectedRows = []
            }
            if (this.selectedRowKeys.indexOf(record.id) <= -1) {
              this.selectedRows.push(record)
              this.selectedRowKeys.push(record.id)
            } else {
              let index = this.selectedRowKeys.indexOf(record.id)
              this.selectedRowKeys.splice(index, 1)
              this.selectedRows.splice(index, 1)
            }
          },
        },
      }
    },
    reSend() {
      if (this.selectedRowKeys.length != 1) {
        this.$message.warning('请选择单条记录进行操作')
        return
      }
      let that = this
      Modal.confirm({
        title: '确认重发',
        content: '此操作将再次发送邮件消息, 是否继续?',
        onOk: function () {
          postAction(that.urls.reSend + '?id=' + that.selectedRowKeys[0]).then((res) => {
            if (res.success) {
              that.$message.success('操作成功')
              that.fetchList()
            } else {
              that.$message.warning('重发出错:', res.message)
            }
          })
        },
      })
    },
    edit() {
      if (this.selectedRowKeys.length != 1) {
        this.$message.warning('请选择单条记录进行操作')
        return
      }
      this.$router.push('/emailLogEdit?id=' + this.selectedRowKeys[0])
    },

    handleSave() {
      this.visible = false
      let json = this.$refs.jsonEditor.getValue()
      if (json) {
        let jsonStr = JSON.stringify(json)
        json = json.replace('"', '"')
        json = json.replace('\n', '')
        json = json.replace('\t', '')
        this.record.taskAfterHandlerVo = json
      }

      postAction(this.urls.edit, this.record).then((res) => {
        if (res.success) {
          this.$message.success('保存成功')
          this.fetchList()
        } else {
          this.$message.success('保存失败:' + res.message)
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
@import '~@assets/less/hte-main.less';
.container {
  .content {
    margin-top: 20px;

    section {
      margin-bottom: 18px;

      .section-table {
        .add-btn {
          margin-left: 0;
          margin-bottom: 10px;
        }

        /deep/ .a-upload__input {
          display: none !important;
        }
      }
    }
  }
}

.tips-box {
  margin-top: 32px;

  .add-btn {
    width: 180px;
    height: 40px;
    line-height: 40px;
    background-color: #4095e4;
    border-radius: 2px;
  }
}

/deep/ .el-upload__input {
  display: none !important;
}
</style>
